<?php

require_once 'classes/PostGenericAction.php';

class PostSend extends PostGenericAction {

    var $constructor_args = array();
    var $default_context = 9; // LP_CONTEXT_INDEX | LP_CONTEXT_POST;
    var $description = 'Add a link to send a post by email';
    
    var $active = true;
    var $hooks = array('parse_post', 'render_comments');
    var $_actions = array('send_post', 'notify_post');
    
    var $_empty_fields;
    
    function PostSend(&$frontend, $args, $dummy_run=false) {
        $this->PostGenericAction($frontend, $args, $dummy_run);
    }
        
    function hide() {
        $this->_frontend->tpl->setVar('PLUGIN_POSTSEND', ' ');
    }
    
    function _send_message(&$post) {

        $tpl =& $this->_frontend->tpl;
        $options =& $this->_frontend->options;
        
        $mh =& $this->_frontend->getMailHelper();
        $shortname = $mh->htent2qp($options['shortname']);
        $shortname_hdr = $mh->encoded2hdr($options['shortname']);
        $header_dashes = '';
        for ($i=0; $i<strlen($options['blogname']); $i++)
            $header_dashes .= '-';
        $tpl->setVar(array(
            'notify_header_dashes'  => $header_dashes,
            'notify_post_title'     => $mh->htent2qp($post['post_title']),
            'notify_post_name'      => $mh->htent2qp($post['post_name']),
            'notify_post_permalink' => $mh->htent2qp($post['post_permalink']),
            'notify_post_date'      => $mh->htent2qp($post['post_date']),
            'notify_post_time'      => $mh->htent2qp($post['post_time']),
            'notify_sender_name'    => $mh->htent2qp($this->_user_data['ud_name']),
            'notify_sender_email'   => $mh->htent2qp($this->_user_data['ud_email']),
            'notify_sender_message' => $mh->htent2qp(wordwrap(strip_tags($this->_user_data['ud_text']), 75, "\r\n"))
            )); 
        $headers = array(
            'Content-Type: text/plain; charset="' . $options['charset'] . '"',
            'MIME-Version: 1.0',
            'Content-Transfer-Encoding: quoted-printable',
            'From: "['. $shortname_hdr . '] ' . $mh->encoded2hdr($this->_user_data['ud_name'], $options['charset']) . '" <' . $this->_user_data['ud_email'] . '>');
        $tpl->setFile('plugin_postsend', 'plugins/post_send.txt');
        $mh->send_mail(
            $this->_user_data["ud_recipients"],
            "[$shortname_hdr] " . $mh->encoded2hdr($this->_frontend->messages['post_mail_subject']),
            $tpl->parse('PLUGIN_POSTSEND', 'plugin_postsend'),
            implode("\r\n", $headers));

        // redirect user so that he does not insert the comment twice
        $this->_frontend->redirectToPost($post['post_permalink'], $this->_frontend->messages['mail_sent'], $this->_remember);
    }
    
    function _check_input() {

        if (is_null($this->_user_data))
            $this->_bootstrap($post, $this->_frontend->messages['comment_form_message']);

        if (!is_null($this->_empty_fields))
            return true;

        if (!in_array($this->_action, $this->_actions))
            return;
        
        // check user submitted data
        $empty_fields = array();
        foreach (array('name', 'email', 'text', 'recipients') as $key) {
            if (empty($this->_user_data["ud_$key"]))
                $empty_fields[] = $key;
        }
        $this->_empty_fields =& $empty_fields;
        return true;
    }
    
    function run($hook, &$post) {
        $tpl =& $this->_frontend->tpl;
        $action =& $this->_action;
        $messages =& $this->_frontend->messages;
        $options =& $this->_frontend->options;
        $message =& $this->_message;
        $message_class =& $this->_message_class;
        $labels =& $this->_labels;
        
        // bootstrap generic action so that we have the user vars
        if (!$this->_check_input())
            return $this->hide();

        if ($hook == 'render_comments') {
            // we have all the input, we will send the email in the parse_post
            // hook, as comments might not be enabled for this post
            if ($action == 'send_post' && count($this->_empty_fields) == 0)
                return;
            // we do not have all the input, let's hide comments and the comment form
            $post['skip_form'] = $post['skip_comments'] = true;
        
        } else if ($hook == 'parse_post') {
            
            // if we have all the input, send the email and redirect the user
            if ($action == 'send_post' && count($this->_empty_fields) == 0)
                $this->_send_message($post);
            // we have errors, let's set warnings
            if ($action == 'send_post') {
                foreach ($this->_empty_fields as $empty_field)
                    $this->_user_data["ud_label_$empty_field"] = "warning";
                $message_class = 'comment_form_warning';
            }
            
            // now display the form
            $message = $messages['comment_required_fields']; // . implode(", ", $empty_fields);
            $post['skip_form'] = $post['skip_comments'] = true;
            $tpl->setVar($this->_user_data);
            $tpl->setVar(array(
                'comment_form_message'          => $message,
                'comment_form_message_class'    => $message_class));
            $tpl->setFile('plugin_postsend', 'plugins/post_send.xml');
            $tpl->parse('PLUGIN_POSTSEND', 'plugin_postsend');
            
        }

    }
    
}

?>